﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web;

namespace SVTTranslateTextEngine.Core.Entries
{
    /// <summary>
    /// This class saves the information about Translation is error or exception.
    /// </summary>
    public class LogInfo
    {
        public const char SPLIT_POINT = ',';
        public const string HOUR_KEY="HH", DATE_STRING="yyyyMMdd";

        /// <summary>
        /// the object create datetime.
        /// </summary>
        public DateTime dtCreateDate { get; set; }

        /// <summary>
        /// the IP Address of request
        /// </summary>
        public string szRequestIpAddress { get; set; }

        /// <summary>
        /// the name of function which run on error or exception
        /// </summary>
        public string szFunctionName { get; set; }

        /// <summary>
        /// the source context which to translates by engine.
        /// </summary>
        public string szFromText { get; set; }

        /// <summary>
        /// the message of the error information
        /// </summary>
        public string szErrorMessage { get; set; }

        /// <summary>
        /// the message of the exception information
        /// </summary>
        public string szExceptionMessage { get; set; }

        /// <summary>
        /// Get the data table structure
        /// </summary>
        /// <returns></returns>
        public static DataTable GetDataTableStructure()
        {
            var dataStructure = new DataTable();
            var ps = typeof(LogInfo).GetProperties();
            foreach(PropertyInfo p in ps)
            {
                if (p.CanWrite && (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(String)))
                    dataStructure.Columns.Add(p.Name,p.PropertyType);
            }
            return dataStructure;
        }

        /// <summary>
        /// To the string for saving to CSV file 
        /// </summary>
        /// <returns>the log record as string</returns>
        public override string ToString()
        {
            var sbContext = new StringBuilder();
            var ps = GetType().GetProperties();
            foreach (PropertyInfo p in ps)
            {
                if (p.CanWrite && (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(String)))
                {
                    var appValue = "";
                    if (p.PropertyType == typeof(DateTime))
                        appValue = (Convert.ToDateTime(p.GetValue(this, null)).ToString("yyyy-MM-dd HH:mm:ss"));
                    else
                        appValue = (Convert.ToString(p.GetValue(this, null)));
                    if (sbContext.Length > 0)
                        sbContext.Append(SPLIT_POINT);
                    var tempUrlDecode = HttpUtility.UrlDecode(appValue);
                    // if the appValue has encoded , the length will different of the tempUrlDecode
                    sbContext.Append(tempUrlDecode.Length == appValue.Length ? HttpUtility.HtmlEncode(appValue) : appValue);
                }
            }
            return sbContext.ToString();
        }
    }
}